from django.shortcuts import render

# Create your views here.
from user import models,userser
from rest_framework.viewsets import ModelViewSet
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.pagination import PageNumberPagination
from django_filters import filters,FilterSet
from django_filters.rest_framework import DjangoFilterBackend

class Page(PageNumberPagination):
    page_size_query_param = 'size'

class Filter(FilterSet):
    name = filters.CharFilter(field_name='name', lookup_expr='icontains')
    class Meta:
        model = models.clas
        fields = ['name',"teacher","status"]

class UserViewSet(ModelViewSet):
    queryset = models.clas.objects.all()
    serializer_class = userser.ClasSer
    pagination_class = Page
    filter_backends = [DjangoFilterBackend]
    filterset_class = Filter


class TeacherViewSet(ModelViewSet):
    queryset = models.Teacher.objects.all()
    serializer_class = userser.TeacherSer

class Update(APIView):
    def put(self,request,pk):
        cls = models.clas.objects.filter(id=pk).first()
        cls.status = not cls.status
        cls.save()
        return Response({"msg":"ok"})
